﻿#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

typedef int SLTDateTydf;

typedef struct List
{
	int data;
	struct List* next;

}LTNode;

void ListNodePushBack(LTNode** phead, SLTDateTydf x);
LTNode* SListNodeFind(LTNode* phead, SLTDateTydf x);
void SListNodePrint(LTNode* phead);


LTNode* BuyNewNode(int x)
{
	//向内存申请空间
	LTNode* newnode = (LTNode*)malloc(sizeof(LTNode));
	assert(newnode);
	newnode->data = x;
	newnode->next = NULL;//尾插及让链表指向的地址为空
	//判断最开始的地址是否为空
	return newnode;
}

void ListNodePushBack(LTNode** phead, SLTDateTydf x)
{
	assert(phead);
	LTNode* newnode = BuyNewNode(x);
	if (*phead == NULL)
	{
		*phead = newnode;
	}
	else
	{
		LTNode* tail = *phead;
		while (tail->next != NULL)
		{
			tail = tail->next;
		}
		tail->next = newnode;
	}
}

LTNode* SListNodeFind(LTNode* phead, SLTDateTydf x)
{
	LTNode* find = phead;
	while (find)//遍历整个链表
	{
		if (find->data == x)//当该节点的值为x时，返回节点地址
		{
			return find;
		}
		else//如果不是，找下一个节点
		{
			find = find->next;
		}
	}
	//都没有找到，返回NULL
	return NULL;
}

void SListNodePrint(LTNode* phead)
{

	LTNode* cur = phead;
	while (cur != NULL)
	{
		printf("%d->", cur->data);
		cur = cur->next;
	}
	printf("->NULL\n");
}

void DestroySList(LTNode** head)
{
	while (*head)
	{
		LTNode* next = (*head)->next;
		free(*head);
		*head = next;
	}
}